<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <title>home</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/swiper.min.css" />
    <style type="text/css">
        .aui-searchbar {
            background-color: rgba(0, 0, 0, 0.0);
            width: 90% !important;
            margin: 0 auto !important;
            height: 2rem !important;
        }

        .aui-bar-nav {
            background-color: rgba(0, 0, 0, 0.0);
        }

        #body {
            background: -webkit-linear-gradient(rgba(0, 0, 255, 0.7), rgba(138, 43, 226, 0.6));
            background: -o-linear-gradient(rgba(0, 0, 255, 0.7), rgba(138, 43, 226, 0.6));
            background: -moz-linear-gradient(rgba(0, 0, 255, 0.7), rgba(255, 255, 255, 1), rgba(255, 255, 255, 1));
            background: url(../image/headerbg0.png) no-repeat, linear-gradient(rgba(0, 0, 255, 1), rgba(255, 255, 255, 1), rgba(255, 255, 255, 1), rgba(255, 255, 255, 1));
            background-position: top left;
            background-size: 100%;
            background-repeat: no-repeat;
            z-index: 999;
            overflow-y: hidden;
        }

        .bodycss .swiper-wrapper {
            background-color: rgba(0, 0, 0, 0.0) !important;
        }

        .bodycss .swiper-slide {
            background-position: top left;
            background-size: contain;
            background-repeat: no-repeat;
            -webkit-background-size: contain;
        }

        #aui-header {
            position: absolute;
            background-color: rgba(0, 0, 0, 0.0);
        }

        .aui-border-input-radius {
            border-radius: 20px;
        }
    </style>
</head>

<body id="body">
    <header class="aui-bar aui-bar-nav " id="aui-header">
        <a class="aui-pull-left aui-btn">
            <span class=""></span>
        </a>
        <div class="aui-title" style="left:0;right:0;" id="aui-title">
            <div class="aui-searchbar" id="search">
                <div class="aui-searchbar-input aui-border-input-radius aui-bg-tag-money" tapmode onclick="doSearch()">
                    <i class="aui-iconfont aui-icon-search"></i>
                    <form>
                        <input type="search" placeholder="请输入搜索内容" id="search-input" disabled="true">
                    </form>
                </div>
            </div>
        </div>
    </header>
    <!-- Swiper -->
    <div class="swiper-container bodycss" id="banner-swiper">
        <div class="swiper-wrapper" id="bannerbgpic">
            <div class="swiper-slide" style="background-image:url('../image/default_headerbg')"><img class="imagecache" src="../image/default_headerbg.png" /></div>
        </div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/config.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/zepto.min.js"></script>
<script type="text/javascript" src="../script/swiper.min.js"></script>
<script type="text/javascript" src="../script/jdpddmenu.js"></script>
<script type="text/javascript">
    var headerPos_h, aui_title_h, swiper, start = 0,
        appSetInfo, the_menulist,
        t1 = 0,
        the_one_menulist = [];
    apiready = function() {
        showProgress('加载中...');
        api.parseTapmode();
        api.setStatusBarStyle({
            style: 'light',
            color: 'rgba(0,0,0,0.0)',
        });
        var headers = $api.byId('aui-header');
        $api.fixStatusBar(headers);
        var headerPos = $api.offset(headers);
        headerPos_h = headerPos.h;
        aui_title_h = $api.offset($api.byId('aui-title')).h;
        the_one_menulist[0] = {
            title: '优选',
            bg: 'rgba(0,0,0,0.0)',
            cid: 0,
            bgSelected: 'widget://image/cate_bg.png'
        };
        hasPermission();
        checkOtherApp();
        getBannerBgPic();
        getAppSet(function(ret, err) {
            hideProgress();
            if (ret && ret.code == 1) {
                $api.setStorage('appSetInfo', ret.data);
                appSetInfo = ret.data;
            } else {
                appSetInfo = $api.getStorage('appSetInfo');
            }
            the_menulist = appSetInfo.indexshowtype == 'tb' ? TB_MENU : appSetInfo.indexshowtype == 'jd' ? JD_MENU : PDD_MENU;
            for (let i = 0; i < the_menulist.length; i++) {
                if (the_menulist[i].parent_id == 0) {
                    the_menulist[i].title = the_menulist[i].cname;
                    the_menulist[i].bg = 'rgba(0,0,0,0.0)';
                    the_menulist[i].bgSelected = 'widget://image/cate_bg.png';
                    the_one_menulist.push(the_menulist[i]);
                }
            }
            setNavMenu('index', headerPos_h, the_one_menulist, {
                width: 68
            });
            initfrmeGroup();
        });
        //pushSend(); //监听消息
        api.addEventListener({
            name: 'offline'
        }, function(ret, err) {
            openWinto('offline_win', 'widget://html/coupon_head', {
                name: $api.getStorage('appSetInfo').app_name || '优券联盟',
                url: 'widget://html/offline.html',
                css: 'aui-hide'
            });
        });
        api.addEventListener({
            name: 'noticeclicked'
        }, function(ret, err) {
            if (ret.value) {
                openindexfenleiurl_new(ret.value);
            }
        });
        api.addEventListener({
            name: 'keyback'
        }, function(ret, err) {
            var time_now = Date.parse(new Date());
            if (time_now - t1 > 1000) {
                t1 = time_now;
                showToast("连续按2次回退键,关闭APP");
            } else {
                api.closeWidget({
                    silent: true
                });
            }
        });
        api.addEventListener({
            name: 'resume'
        }, function(ret, err) {
            //location.reload();
        });
        api.addEventListener({
            name: 'online'
        }, function(ret, err) {
            api.closeToWin({
                name: 'root'
            });
            api.closeFrameGroup({
                name: 'index_frame_list'
            });
            api.closeFrameGroup({
                name: 'menulist_frame_list'
            });
            api.closeFrameGroup({
                name: '9.9_frame_list'
            });
            api.closeFrameGroup({
                name: 'brand_frame_list'
            });
            api.setTabBarAttr({
                index: 0,
            });
            var NVNavigationBar = api.require('NVNavigationBar');
            NVNavigationBar.close({
                id: 'index'
            });
            location.reload();
        });
        api.addEventListener({
            name: 'tabitembtn'
        }, function(ret) {
            api.setFrameGroupAttr({
                name: 'index_frame_list',
                hidden: true
            });
            api.setFrameAttr({
                name: 'webcoupon_9.9by',
                hidden: true
            });
            api.setFrameGroupAttr({
                name: 'menulist_frame_list',
                hidden: true
            });
            api.setFrameAttr({
                name: 'webcoupon_jfjx',
                hidden: true
            });
            console.log(ret.index);
            if (ret.index == 0) {
                console.log(ret.index);
                api.setFrameGroupAttr({
                    name: 'index_frame_list',
                    hidden: false
                });
                api.setFrameGroupIndex({
                    name: 'index_frame_list',
                    index: 0,
                    scroll: true
                });
            } else if (ret.index == 1) {
                api.setFrameGroupAttr({
                    name: 'menulist_frame_list',
                    hidden: false
                });
            } else if (ret.index == 2) {
                api.setFrameAttr({
                    name: 'webcoupon_jfjx',
                    hidden: false
                });
            } else if (ret.index == 3) {
                api.setFrameAttr({
                    name: 'webcoupon_9.9by',
                    hidden: false
                });
            } else {
                var jsfun = 'location.reload();';
                api.execScript({
                    name: 'root',
                    frameName: 'my_win',
                    script: jsfun
                });
            }
            api.setTabBarAttr({
                index: ret.index
            });
        });
    }

    function initfrmeGroup() {
        var isShow = false;
        var frame_list = [{
            name: 'index_frame_0',
            url: './index_main_frm.html',
            pageParam: {
                cid: 0,
                source_type: appSetInfo.indexshowtype
            },
            bounces: true,
            allowEdit: true
        }];
        for (var i = 1; i < the_one_menulist.length; i++) {
            frame_list[i] = {
                name: 'index_frame_' + appSetInfo.indexshowtype + '_' + i,
                url: './index_frames.html',
                pageParam: {
                    cid: the_one_menulist[i].cid,
                    index: i,
                    source_type: appSetInfo.indexshowtype
                },
                bounces: true,
                allowEdit: true,
            }
        }

        api.openFrameGroup({
            name: 'index_frame_list',
            scrollEnabled: true,
            rect: {
                marginLeft: 0,
                marginRight: 0,
                marginTop: headerPos_h + 42,
                marginBottom: 54
            },
            fixedOn: 'ui_layout',
            index: 0,
            preload: 5,
            frames: frame_list
        }, function(ret, err) {
            ret.index == 0 && isShow == false && getIndexShow();
            isShow = true;
            setNavMenuSelected('index', ret.index);
        });
    }

    function getBannerBgPic() {
        var div = '';
        var bgdata = getStorage('getBanner');
        if (!bgdata) {
            postData(API_URL + 'getBanner', {
                apiname: 'getBanner'
            }, function(ret, err) {
                if (ret.code == 1) {
                    setStorage('getBanner', ret.data);
                    bgdata = ret.data;
                    for (var i = 0; i < bgdata.length; i++) {
                        if (!bgdata[i].bgpic) {
                            bgdata[i].bgpic = "../image/default_headerbg.png";
                        }
                        div += '<div class="swiper-slide" style="background-image:url("../image/default_headerbg")"><img class="imagecache" src="' + bgdata[i].bgpic +
                            '" onerror="this.src=../image/default_headerbg.png"/></div>';
                    }
                    $api.html($api.byId('bannerbgpic'), div);
                    iCache($('.imagecache'), '../image/default_headerbg.png');
                    swiper = new Swiper('.swiper-container', {
                        effect: 'fade',
                        fadeEffect: {
                            crossFade: true,
                        }
                    });
                } else {
                    showToast('网络错误~');
                }
            });
        } else {
            for (var i = 0; i < bgdata.length; i++) {
                if (!bgdata[i].bgpic) {
                    bgdata[i].bgpic = "../image/default_headerbg.png";
                }
                div += '<div class="swiper-slide" style="background-image:url("../image/default_headerbg")"><img class="imagecache" src="' + bgdata[i].bgpic +
                    '" onerror="this.src=../image/default_headerbg.png"/></div>';
            }
            $api.html($api.byId('bannerbgpic'), div);
            iCache($('.imagecache'), '../image/default_headerbg.png');
            swiper = new Swiper('.swiper-container', {
                effect: 'fade',
                fadeEffect: {
                    crossFade: true,
                }
            });
        }

    }

    //改变banner背景图
    function changeBannerBg(data) {
        swiper.slideTo(data, 1000, false);
    }

    function getIndexShow() {
        postData(API_URL + 'getIndexShow', {}, function(ret, err) {
            if (ret) {
                if (ret.code == 1) {
                    if (ret.data.pic) {
                        var pos = ret.data.pic.lastIndexOf("/");
                        var filename = ret.data.pic.substring(pos + 1);
                        var path = api.cacheDir + "/pic/" + filename;
                        var obj = api.require('fs');
                        obj.exist({
                            path: path
                        }, function(rets, err) {
                            if (rets.exist) {
                                if (rets.directory) {} else {
                                    showTipInfo(path, ret.data.url);
                                }
                            } else {
                                api.download({
                                    url: ret.data.pic,
                                    savePath: path,
                                    report: false,
                                    cache: true,
                                    allowResume: true
                                }, function(retd, err) {
                                    if (retd.state == 1) {
                                        showTipInfo(path, ret.data.url);
                                    }
                                });
                            }
                        });

                    }
                }
            }
        })
    }

    //活动提示
    function showTipInfo(image, url) {
        var dialogBox = api.require('dialogBox');
        dialogBox.discount({
            rect: {
                w: api.winWidth * 2.5 / 3,
                h: (api.winWidth * 2.2 / 3) * 4 / 3
            },
            styles: {
                bg: '#FFF',
                image: {
                    path: image ? image : 'widget://image/showTipInfo.png',
                    marginB: api.winHeight / 3
                },
                cancel: {
                    icon: 'widget://image/guanbi.png',
                    w: 30,
                    h: 30,
                    marginB: api.winHeight / 3 - 60,
                }
            },
            tapClose: true
        }, function(ret) {
            if (ret.eventType == 'image') {
                dialogBox.close({
                    dialogName: 'discount'
                });
                openindexfenleiurl_new(url);
            } else {
                dialogBox.close({
                    dialogName: 'discount'
                });
            }
        });
    }

    function checkOtherApp() {
        var systemType = api.systemType;
        var jd = systemType == 'ios' ? 'openapp.jdmobile' : "com.jingdong.app.mall";
        var pdd = systemType == 'ios' ? 'pddopen' : "com.xunmeng.pinduoduo";
        var wx = systemType == 'ios' ? "weixin" : "com.tencent.mm";
        api.appInstalled({
            appBundle: pdd
        }, function(ret, err) {
            if (ret.installed) {
                $api.setStorage("pddisinstall", 'true');
            } else {
                $api.setStorage("pddisinstall", 'false');
            }
        });
        api.appInstalled({
            appBundle: jd
        }, function(ret, err) {
            if (ret.installed) {
                $api.setStorage("jdisinstall", 'true');
            } else {
                $api.setStorage("jdisinstall", 'false');
            }
        });
        api.appInstalled({
            appBundle: wx
        }, function(ret, err) {
            if (ret.installed) {
                $api.setStorage("wxisinstall", 'true');
            } else {
                $api.setStorage("wxisinstall", 'false');
            }
        });

    }
    //推送消息监听
    function pushSend() {
        var ajpush = api.require('ajpush');
        ajpush.init(function(ret) {
            console.log(JSON.stringify('极光初始化:' + ret.status));
        });
        ajpush.getRegistrationId(function(ret) {
            console.log('极光ID:' + ret.id);
        });
        ajpush.setListener(
            function(ret) {
                console.log("监听到极光推送消息：" + JSON.stringify(ret));
                var id = ret.id;
                var title = ret.title;
                var content = ret.content;
                var extra = ret.extras ? ret.extras : ret.extra;
                var url = extra.url || '';
                notification(title, content, url);
            }
        );
    }
    // 监听到的消息转为状态栏通知
    function notification(t, c, e) {
        api.notification({
            vibrate: [300, 500], //震动时间节奏
            sound: 'default', //系统默认提示音
            light: false, //是否亮灯，需设备支持
            notify: { //状态栏通知
                title: t, //标题，默认值为应用名称，只Android有效
                content: c, //内容，默认值为'有新消息'
                extra: e, //传递给通知的数据，在通知被点击后，该数据将通过监听函数回调给网页
                updateCurrent: false //是否覆盖更新已有的通知，取值范围true|false。只Android有效
            }
        }, function(ret, err) {
            console.log(JSON.stringify(ret));
        });
    }
</script>

</html>
